package org.agilelang.generator.cakephp;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.MenuItem;
import org.agilelang.object.attributes.Reference;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.ViewReport;

public class GControllerScaffold extends GControllerSimple {

    String menu = "";
    String submenu = "";

    GControllerScaffold(String path, Project project, Controller controller) {
        super(path, project, controller);
        this.path = path;
        this.controllers = project.controllers;
        this.menus = project.menus;
        this.appName = project.application.name;
        this.controller = controller;
        this.models = project.models;
        setTemplate2("cakephp");
        this.template = "bootstrap";
    }

    @Override
    public void process() {
        String textfield_reference = "";
        String model_reference = "";
        Model model = this.getModelByName(controller.model_scaffold);
        int tr = model.references.size();
        for (int j = 0; j < tr; j++) {
            Reference reference = model.references.get(j);
            textfield_reference = textfield_reference + "\t\t$this->set('" + reference.foreign_key + "',$this->" + reference.model_reference + "->find('list',array()));\n";
            uses = uses + ",'" + reference.model_reference + "'";
            model_reference = model_reference
                    + "\t\t'" + reference.model_reference + "' => array(\n"
                    + "\t\t\t'className' => '" + reference.model_reference + "',\n"
                    + "\t\t\t'foreignKey' => '" + reference.foreign_key + "',\n"
                    + "\t\t\t'conditions' => '',\n"
                    + "\t\t\t'fields' => '',\n"
                    + "\t\t\t'order' => ''\n"
                    + "\t\t),\n";
        }
        String views = "";
        views += view(controller.views, controller.name);
        views += viewform(controller.forms, controller.name);
        //views += viewreport(controller.reports, controller.name);
        views += viewreport_model_describe(controller.reports, controller.name);

        String order = "";
        if (!controller.orderIndex.equals("")) {
            String[] order_array = controller.orderIndex.trim().split(" ");
            order = ",'order' => array('" + order_array[0] + "' => '" + order_array[1] + "' )";
        }
        
        String conditions = "";
        if (!controller.conditionsIndex.equals("")) {
            String[] condition_array = controller.conditionsIndex.trim().split(",");
            conditions = ",'conditions' => array('" + condition_array[0] + "' => '" + condition_array[1] + "' )";
            
        }

        String file = readStreamTpl("cs_controller.php.tpl");
        file = file.replace("//views_str//", views);
        file = file.replace("//order//", order);
        file = file.replace("//conditions//", conditions);

        file = file.replace("//nameClass//", controller.name);
        file = file.replace("//nameClass_Singularize//", controller.model_scaffold);

        file = file.replace("//nameclass//", controller.name.toLowerCase());
        file = file.replace("//nameclass_singularize//", controller.model_scaffold.toLowerCase());

        file = file.replace("//nameCaption//", controller.name);
        file = file.replace("//uses//", "\tpublic $uses = array('" + controller.model_scaffold + "'" + uses + ");\n");
        file = file.replace("//textfield_reference//", textfield_reference);

        search_menu4(controller.name, "crud");
        file = file.replace("//menu//", menu.toLowerCase());
        file = file.replace("//menuitem//", submenu.toLowerCase());

        createfile("Controller/" + controller.name + "Controller.php", path, file);
        createDirectory(path + File.separator + "view/" + controller.name);

        //index(controller.name, model.caption, model.name, model.attributes, model.references);
        index(controller, controller.name, controller.caption, model.name, model.attributes, model.references);
        create(controller.name, controller.caption, model.name, model.attributes, model.references);
        read(controller.name, controller.caption, model.name, model.attributes, model.references);
        update(controller.name, controller.caption, model.name, model.attributes, model.references);

        //------------------------------------------------------------------
        /*file = readStreamTpl("model.tpl");
         file = file.replace("//nameClass//", model.name);
         file = file.replace("//belongsTo//", "\tvar $belongsTo = array(\n" + model_reference + "\n\t);");
         createfile("model/" + model.name + ".php", path, file);*/
    }

    private void index(Controller controller, String name, String nameCaption, String name_singularize, List<Attribute> attrs, List<Reference> references) {
        int tt = attrs.size();
        String td_attrs = "";
        String th_attrs = "";

        if (!this.controller.attributesIndex.equals("")) {
            String[] attr_str = this.controller.attributesIndex.split(",");
            int t = attr_str.length;
            for (int j = 0; j < t; j++) {
                Attribute attr = search_attr(attr_str[j].trim(), attrs);
                if (attr.name.endsWith("_id")) {
                    int tr = references.size();
                    for (int jj = 0; jj < tr; jj++) {
                        Reference reference = references.get(jj);
                        if (attr.name.equals(reference.foreign_key)) {
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']; ?>&nbsp;</td> \r\n";
                            break;
                        }
                    }
                } else {
                    switch (attr.type) {
                        case "boolean":
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'] == '1' ? 'Si' :'No'; ?>&nbsp;</td> \r\n";
                            break;
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "case " + (z + 1) + ": echo  '" + values[z] + "';break;";
                            }
                            td_attrs = td_attrs + "\t\t\t<td>"
                                    + "<?php switch($" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "']) {"
                                    + options
                                    + "}; ?>&nbsp;</td> \r\n";
                            break;
                        default:
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "']; ?>&nbsp;</td> \r\n";
                            break;
                    }
                }



                th_attrs = th_attrs + "\t\t\t<th><?php echo $this->Paginator->sort('" + attr.caption + "');?></th>\r\n";
            }

        } else {
            Collections.sort(attrs, new CustomComparator2());
            for (int j = 0; j < tt; j++) {
                Attribute attr = attrs.get(j);
                if (attr.name.endsWith("_id")) {
                    int tr = references.size();
                    for (int jj = 0; jj < tr; jj++) {
                        Reference reference = references.get(jj);
                        if (attr.name.equals(reference.foreign_key)) {
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']; ?>&nbsp;</td> \r\n";
                            break;
                        }
                    }
                } else {
                    switch (attr.type) {
                        case "boolean":
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'] == '1' ? 'Si' :'No'; ?>&nbsp;</td> \r\n";
                            break;
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "case " + (z + 1) + ": echo  '" + values[z] + "';break;";
                            }
                            td_attrs = td_attrs + "\t\t\t<td>"
                                    + "<?php switch($" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "']) {"
                                    + options
                                    + "}; ?>&nbsp;</td> \r\n";
                            break;
                        default:
                            td_attrs = td_attrs + "\t\t\t<td><?php echo $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "']; ?>&nbsp;</td> \r\n";
                            break;
                    }
                }



                th_attrs = th_attrs + "\t\t\t<th><?php echo $this->Paginator->sort('" + attr.caption + "');?></th>\r\n";
            }

        }

        String action_report = "";
        List<ViewReport> reports = controller.reports;
        int tr = reports.size();
        for (int x = 0; x < tr; x++) {
            if (reports.get(x).type.equals("model_describe")) {
                action_report += "<?php echo $this->Html->link('<i class=\"icon-file\"></i>' . __('" + reports.get(x).caption + "'), array('action' => '" + reports.get(x).name + "', $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['id']), array('escape' => FALSE)); ?>\n";
            }
        }



        String file = readStreamTpl("cs_index.html.tpl");
        //file = file.replace("//appName//", this.appName);
        file = file.replace("//nameClass_pluralize//", name);
        file = file.replace("//nameCaption//", nameCaption);
        file = file.replace("//nameClass_singularize//", name_singularize);
        file = file.replace("//nameclass_pluralize//", name.toLowerCase());
        file = file.replace("//nameclass_singularize//", name_singularize.toLowerCase());
        file = file.replace("//td_attrs//", td_attrs);
        file = file.replace("//th_attrs//", th_attrs);

        file = file.replace("//action_report//", action_report);
        createfile("view/" + name.toLowerCase() + "/index.ctp", path, file);
    }

    public void create(String name, String nameCaption, String name_singularize, List<Attribute> attrs, List<Reference> references) {
        int tt = attrs.size();
        String textfield_attrs = "";
        String[] defaults_array = this.controller.defaultsAdd.split(",");
        if (!this.controller.attributesAdd.equals("")) {
            String[] attr_str = this.controller.attributesView.split(",");
            

            int t = attr_str.length;
            for (int j = 0; j < t; j++) {
                Attribute attr = search_attr(attr_str[j].trim(), attrs);
                if (attr.name.endsWith("_id")) {
                    if (defaults_array[0].equals(attr.name)) {
                    } else {
                    }
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type' => 'select', 'options' => $" + attr.name + ",'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "'));?>\r\n";
                } else {
                    switch (attr.type) {
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "'" + (z + 1) + "' => '" + values[z] + "',";
                            }
                            if (defaults_array[0].equals(attr.name)) {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                        + "'type' => 'select', "
                                        + "'options' => array(" + options + "), "
                                        + "'selected' => '" + defaults_array[1] + "',"
                                        + "'style' => 'display:none',"
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label','style' => 'display:none'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            } else {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                        + "'type' => 'select', "
                                        + "'options' => array(" + options + "), "
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            }

                            break;
                        case "text":
                            if (defaults_array[0].equals(attr.name)) {
                                textfield_attrs += "<?php echo "
                                        + "$this->Form->input('" + attr.name + "', "
                                        + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 140px;',"
                                        + "'value' => '" + defaults_array[1] + "',"
                                        + "'disabled' => 'disabled',"
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            } else {
                                textfield_attrs += "<?php echo "
                                        + "$this->Form->input('" + attr.name + "', "
                                        + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 140px;',"
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            }

                            break;
                        default:
                            if (defaults_array[0].equals(attr.name)) {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', "
                                        + "array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'value' => '" + defaults_array[1] + "',"
                                        + "'style' => 'display:none',"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            } else {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', "
                                        + "array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            }

                            break;
                    }
                }
            }
        } else {
            Collections.sort(attrs, new CustomComparator2());
            for (int j = 0; j < tt; j++) {
                Attribute attr = attrs.get(j);
                if (attr.name.endsWith("_id")) {
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type' => 'select', 'options' => $" + attr.name + ",'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "'));?>\r\n";
                } else {
                    switch (attr.type) {
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "'" + (z + 1) + "' => '" + values[z] + "',";
                            }
                            
                            if (defaults_array[0].equals(attr.name)) {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                        + "'type' => 'select', "
                                        + "'options' => array(" + options + "), "
                                        + "'selected' => '" + defaults_array[1] + "',"
                                        + "'style' => 'display:none',"
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label','style' => 'display:none'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            } else {
                                textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                        + "'type' => 'select', "
                                        + "'options' => array(" + options + "), "
                                        + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                        + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            }

                            
                            
                            break;
                        case "text":
                            textfield_attrs += "<?php echo "
                                    + "$this->Form->input('" + attr.name + "', "
                                    + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 140px;',"
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'placeholder' => '" + attr.caption + "')); ?>\r\n";

                            break;
                        default:
                            textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', "
                                    + "array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            break;
                    }

                }
            }
        }

        String file = readStreamTpl("cs_create.html.tpl");
        file = file.replace("//appName//", this.appName);
        file = file.replace("//nameCaption//", nameCaption);
        file = file.replace("//nameClass_pluralize//", name);
        file = file.replace("//nameClass_singularize//", name_singularize);
        file = file.replace("//nameclass_pluralize//", name.toLowerCase());
        file = file.replace("//nameclass_singularize//", name_singularize.toLowerCase());
        file = file.replace("//nameclass//", name.toLowerCase());
        file = file.replace("//textfield_attrs//", textfield_attrs);
        createfile("view/" + name.toLowerCase() + "/add.ctp", path, file);
    }

    public void read(String name, String nameCaption, String name_singularize, List<Attribute> attrs, List<Reference> references) {
        Collections.sort(attrs, new CustomComparator2());

        String textfield_attrs = "";
        if (!this.controller.attributesView.equals("")) {
            String[] attr_str = this.controller.attributesView.split(",");
            int t = attr_str.length;
            for (int j = 0; j < t; j++) {
                Attribute attr = search_attr(attr_str[j].trim(), attrs);
                String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());

                if (attr.name.endsWith("_id")) {
                    int tr = references.size();
                    for (int jj = 0; jj < tr; jj++) {
                        Reference reference = references.get(jj);
                        if (attr.name.equals(reference.foreign_key)) {
                            /*textfield_attrs = textfield_attrs
                             + "<tr>\r\n"
                             + "<td><?php echo __('" + attr.caption + "'); ?> : </td>"
                             + "<td><?php echo h($" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']); ?></td>\r\n"
                             + "</tr>\r\n";*/
                            textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type'=>'text','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'disabled' => 'disabled','value'=>$" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "'])); ?>\r\n";
                            break;
                        }
                    }
                } else {
                    /*textfield_attrs = textfield_attrs
                     + "<tr>\r\n"
                     + "<td><?php echo __('" + attr.name + "'); ?> : </td>"
                     + "<td><?php echo h($" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "']); ?></td>\r\n"
                     + "</tr>\r\n";*/
                    switch (attr.type) {
                        case "boolean":
                            textfield_attrs += "<?php echo "
                                    + "$this->Form->input('" + attr.name + "', "
                                    + "array('type'=>'text','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'disabled' => 'disabled',"
                                    + "'value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'] == '1' ? 'Si' :'No')); ?>\r\n";
                            break;
                        case "text":
                            textfield_attrs += "<?php echo "
                                    + "$this->Form->input('" + attr.name + "', "
                                    + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 142px;',"
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'disabled' => 'disabled',"
                                    + "'value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'] == '1' ? 'Si' :'No')); ?>\r\n";
                            break;
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "'" + (z + 1) + "' => '" + values[z] + "',";
                            }
                            textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                    + "'type' => 'select', "
                                    + "'disabled' => 'disabled',"
                                    + "'options' => array(" + options + "), "
                                    + "'selected' => $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'], "
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            break;
                        default:
                            textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type'=>'text','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'disabled' => 'disabled','value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'])); ?>\r\n";
                            break;
                    }
                }
            }
        } else {
            int tt = attrs.size();
            for (int j = 0; j < tt; j++) {
                Attribute attr = attrs.get(j);
                String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());

                if (attr.name.endsWith("_id")) {
                    int tr = references.size();
                    for (int jj = 0; jj < tr; jj++) {
                        Reference reference = references.get(jj);
                        if (attr.name.equals(reference.foreign_key)) {
                            /*textfield_attrs = textfield_attrs
                             + "<tr>\r\n"
                             + "<td><?php echo __('" + attr.caption + "'); ?> : </td>"
                             + "<td><?php echo h($" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "']); ?></td>\r\n"
                             + "</tr>\r\n";*/
                            textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type'=>'text','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'disabled' => 'disabled','value'=>$" + name_singularize.toLowerCase() + "['" + reference.model_reference + "']['" + reference.field_reference + "'])); ?>\r\n";
                            break;
                        }
                    }
                } else {
                    switch (attr.type) {
                        case "boolean":
                            textfield_attrs += "<?php echo "
                                    + "$this->Form->input('" + attr.name + "',"
                                    + " array('type'=>'text',"
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'disabled' => 'disabled',"
                                    + "'value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'] == '1' ? 'Si' :'No')); ?>\r\n";
                            break;
                        case "text":
                            textfield_attrs += "<?php echo "
                                    + "$this->Form->input('" + attr.name + "', "
                                    + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 142px;',"
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'disabled' => 'disabled',"
                                    + "'value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'])); ?>\r\n";
                            break;
                        case "enum":
                            String[] values = attr.value.split(",");
                            String options = "";
                            for (int z = 0; z < values.length; z++) {
                                options += "'" + (z + 1) + "' => '" + values[z] + "',";
                            }
                            textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                                    + "'type' => 'select', "
                                    + "'disabled' => 'disabled',"
                                    + "'options' => array(" + options + "), "
                                    + "'selected' => $" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'], "
                                    + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                                    + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                            break;
                        default:
                            textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('type'=>'text','label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'disabled' => 'disabled','value'=>$" + name_singularize.toLowerCase() + "['" + name_singularize + "']['" + attr.name + "'])); ?>\r\n";
                            break;
                    }
                }
            }
        }


        String file = readStreamTpl("cs_read.html.tpl");
        file = file.replace("//appName//", this.appName);
        file = file.replace("//nameCaption//", nameCaption);
        file = file.replace("//nameClass_pluralize//", name);
        file = file.replace("//nameClass_singularize//", name_singularize);
        file = file.replace("//nameclass_pluralize//", name.toLowerCase());
        file = file.replace("//nameclass_singularize//", name_singularize.toLowerCase());
        file = file.replace("//label_attrs//", textfield_attrs);
        createfile("view/" + name.toLowerCase() + "/view.ctp", path, file);
    }

    public void update(String name, String nameCaption, String name_singularize, List<Attribute> attrs, List<Reference> references) {
        Collections.sort(attrs, new CustomComparator2());

        String textfield_attrs = "";
        if (!this.controller.attributesView.equals("")) {
            String[] attr_str = this.controller.attributesView.split(",");
            String[] defaults_str = this.controller.defaultsUpdate.split(",");
            int t = attr_str.length;
            for (int j = 0; j < t; j++) {
                Attribute attr = search_attr(attr_str[j].trim(), attrs);
                String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());

                if (attr.name.endsWith("_id")) {
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'type' => 'select', 'options' => $" + attr.name + "));?>\r\n";
                } else if (attr.type.equals("enum")) {
                    String[] values = attr.value.split(",");
                    String options = "";
                    for (int z = 0; z < values.length; z++) {
                        options += "'" + (z + 1) + "' => '" + values[z] + "',";
                    }
                    textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                            + "'type' => 'select', "
                            + "'options' => array(" + options + "), "
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                            + "'placeholder' => '" + attr.caption + "')); ?>\r\n";

                } else if (attr.type.equals("text")) {
                    textfield_attrs += "<?php echo "
                            + "$this->Form->input('" + attr.name + "', "
                            + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 140px;',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                            + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                    break;
                } else {
                    //textfield_attrs = textfield_attrs + "\techo $this->Form->input('" + attr.name + "');\r\n";
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "')); ?>\r\n";

                }
            }
        } else {
            int tt = attrs.size();
            for (int j = 0; j < tt; j++) {
                Attribute attr = attrs.get(j);
                String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());

                if (attr.name.endsWith("_id")) {
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'type' => 'select', 'options' => $" + attr.name + "));?>\r\n";
                } else if (attr.type.equals("enum")) {
                    String[] values = attr.value.split(",");
                    String options = "";
                    for (int z = 0; z < values.length; z++) {
                        options += "'" + (z + 1) + "' => '" + values[z] + "',";
                    }
                    textfield_attrs += "<?php echo $this->Form->input('" + attr.name + "', array("
                            + "'type' => 'select', "
                            + "'options' => array(" + options + "), "
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                            + "'placeholder' => '" + attr.caption + "')); ?>\r\n";

                } else if (attr.type.equals("text")) {
                    textfield_attrs += "<?php echo "
                            + "$this->Form->input('" + attr.name + "', "
                            + "array('type'=>'textarea','rows' => '6', 'cols' => '80','style' => 'width: 655px; height: 140px;',"
                            + "'label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),"
                            + "'placeholder' => '" + attr.caption + "')); ?>\r\n";
                } else {
                    //textfield_attrs = textfield_attrs + "\techo $this->Form->input('" + attr.name + "');\r\n";
                    textfield_attrs = textfield_attrs + "<?php echo $this->Form->input('" + attr.name + "', array('label'=>array('text'=>'" + attr.caption + "','class' => 'control-label'),'placeholder' => '" + attr.caption + "')); ?>\r\n";

                }
            }
        }

        String file = readStreamTpl("cs_update.html.tpl");
        file = file.replace("//appName//", this.appName);
        file = file.replace("//nameClass_pluralize//", name);
        file = file.replace("//nameCaption//", nameCaption);
        file = file.replace("//nameClass_singularize//", name_singularize);
        file = file.replace("//nameclass_pluralize//", name.toLowerCase());
        file = file.replace("//nameclass_singularize//", name_singularize.toLowerCase());
        file = file.replace("//textfield_attrs//", textfield_attrs);
        createfile("view/" + name.toLowerCase() + "/edit.ctp", path, file);
    }

    private void search_menu4(String controller, String view) {
        int t = menus.size();
        for (int i = 0; i < t; i++) {
            int tt = menus.get(i).menuitems.size();
            for (int ii = 0; ii < tt; ii++) {
                MenuItem menuitem = menus.get(i).menuitems.get(ii);
                if (menuitem.controller.equals(controller) && menuitem.action.equals(view)) {
                    menu = menus.get(i).name;
                    submenu = menuitem.name;
                }
            }
        }
    }

    private String search_menu_view(String controller, String view) {
        String menu = TAB + "$this->set('menu', '');\r\n"
                + TAB + "$this->set('submenu', '');\r\n"
                + TAB + "$this->set('cakeDescription', '');\r\n"
                + TAB + "$this->set('title_for_layout', '');\r\n";
        int t = menus.size();
        for (int i = 0; i < t; i++) {
            int tt = menus.get(i).menuitems.size();
            for (int ii = 0; ii < tt; ii++) {
                MenuItem menuitem = menus.get(i).menuitems.get(ii);
                String action_item = "";
                if (menuitem.controller.equals(controller) && menuitem.action.equals(view)) {
                    menu = TAB + "$this->set('menu', '" + menus.get(i).name.toLowerCase() + "');\r\n"
                            + TAB + "$this->set('submenu', '" + menuitem.name.toLowerCase() + "');\r\n"
                            + TAB + "$this->set('cakeDescription', '');\r\n"
                            + TAB + "$this->set('title_for_layout', '');\r\n";
                }
            }
        }
        return menu;
    }

    public Attribute search_attr(String name, List<Attribute> list) {
        int tt = list.size();
        for (int j = 0; j < tt; j++) {
            Attribute attr = list.get(j);
            if (attr.name.equals(name)) {
                return attr;
            }
        }
        return null;
    }

    public class CustomComparator2 implements Comparator<Attribute> {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.item.compareTo(o2.item);
        }
    }
}